package com.example.test.views;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.api.Constant;

/**
 * Created by Administrator on 2015/11/21.
 */
public class SelectPicturePop {

	Activity context;

	View contentV;

	PopupWindow pop;

	static SelectPicturePop instance;

	TextView photographT, albumT, existingT, cancelT;

	String mPhotoPath;

	OnStateChangeListener onStateChangeListener;

	public SelectPicturePop(Activity context) {

		this.context = context;
		contentV = LayoutInflater.from(context).inflate(
				R.layout.pop_select_picture, null);
		pop = new PopupWindow(contentV, ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT, true);
		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());
		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);
		pop.setAnimationStyle(R.style.PopupLookAround);
		pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		initView();

	}

	public static SelectPicturePop getInstance(Activity context) {
		instance = new SelectPicturePop(context);

		return instance;
	}

	private void initView() {
		photographT = (TextView) contentV.findViewById(R.id.photograph);
		albumT = (TextView) contentV.findViewById(R.id.album);
		existingT = (TextView) contentV.findViewById(R.id.existing);
		cancelT = (TextView) contentV.findViewById(R.id.cancel);
		cancelT.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				pop.dismiss();
			}
		});
		contentV.findViewById(R.id.pop_background).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						pop.dismiss();
					}
				});

		// 从相册选取
		albumT.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onStateChangeListener != null) {
					onStateChangeListener.close(mPhotoPath);
				}
				Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
				getImage.addCategory(Intent.CATEGORY_OPENABLE);
				getImage.setType("image/jpeg");
				context.startActivityForResult(getImage, Constant.PICK_PHOTO);
				pop.dismiss();
			}
		});
		// 拍照
		photographT.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onStateChangeListener != null) {
					onStateChangeListener.close(mPhotoPath);
				}
				Intent getImageByCamera = new Intent(
						"android.media.action.IMAGE_CAPTURE");
				getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT,
						Uri.fromFile(new File(mPhotoPath)));
				context.startActivityForResult(getImageByCamera,
						Constant.TAKE_PHOTO);
				pop.dismiss();
			}
		});
	}

	public void show(File mCacheDir) {

		mPhotoPath = new File(mCacheDir, System.currentTimeMillis() + ".jpg")
				.getAbsolutePath();
		pop.showAtLocation(contentV.getRootView(), Gravity.CENTER, 0, 0);
	}

	public void dismiss() {
		pop.dismiss();
	}

	public interface OnStateChangeListener {
		void close(String photoPath);
	}

	public OnStateChangeListener getOnStateChangeListener() {
		return onStateChangeListener;
	}

	public void setOnStateChangeListener(
			OnStateChangeListener onStateChangeListener) {
		this.onStateChangeListener = onStateChangeListener;
	}

}
